import datetime

from django.shortcuts import get_object_or_404, redirect

from brispon.util.decorators import render_to, ajax_request
from brispon.util import BaseSearchForm, paginate, init_inc_paginate, inc_paginate
from brispon.models import Event
from brispon import brispon_setting as bs

# web

class EventForm(BaseSearchForm):
    class Meta:
        base_qs = Event.objects.exclude(event_type='TEST')
        search_fields = ('searchable',) 
        default_fields = {'!active': True}
        
@render_to('d/event/list.html')
def list(request):
    if request.is_mobile:
        return redirect('brispon.views.event.mlist')
    event_form = EventForm(request.GET or None)
    return init_inc_paginate(request.GET, search_form=event_form)

@render_to('d/event/inc_list.html')
def inc_list(request):
    event_form = EventForm(request.GET or None)
    return inc_paginate(request.GET, search_form=event_form)

@render_to('d/event/inc_main_list.html')
def inc_main_list(request):
    #objects = Event.objects.get_aevents()
    objects = Event.objects.get_aevents_without_mevents()
    return inc_paginate(request.GET, objects=objects)

@render_to('d/event/detail.html')
def detail(request, event_id):
    if request.is_mobile:
        return redirect('brispon.views.event.mdetail', event_id)
    event = get_object_or_404(Event, pk=event_id)
    return {'event':event}

@render_to('d/event/preview.html')
def preview(request, event_id):
    event = get_object_or_404(Event, pk=event_id)
    return {'event':event}

def check_active(request):
    aevents = Event.objects.filter(active=True)
    today = datetime.date.today()
    for aevent in aevents:
        if aevent.edate < today:
            aevent.active = False
            aevent.save()
    return redirect('/admin/brispon/event/')

## web test
@render_to('d/event/list_test.html')
def list_test(request):
    if request.is_mobile:
        return redirect('brispon.views.event.mlist_test')
    events = Event.objects.filter(event_type='TEST')
    return {'events':events}

## mobile 
@render_to('m/event/list.html')
def mlist(request):
    if not request.is_mobile:
        return redirect('brispon.views.event.list')
    event_form = EventForm(request.GET or None)
    return paginate(request.GET, search_form=event_form, unit_page=12)

@render_to('m/event/inc_list.html')
def minc_list(request):
    get_page = request.GET['gp']
    events = Event.objects.all()
    objects = inc_paginate(events, get_page)
    return {'objects':objects}

@render_to('m/event/detail.html')
def mdetail(request, event_id):
    if not request.is_mobile:
        return redirect('brispon.views.event.detail', event_id)
    event = get_object_or_404(Event, pk=event_id)
    return {'event':event}

@render_to('m/event/get_coupon.html')
def mcoupon(request, event_id):
    event = get_object_or_404(Event, pk=event_id)
    if not request.user.is_authenticated() or request.user.is_anonymous():
        return {'result':'Please login first', 
                'event':event}
    success, msg = event.coupon.send_coupon_by_sms(request.user)
    return {'result':msg,
            'event':event}

@render_to('m/event/kakao.html')
def mkakao(request):
    return {}

## mobile test
@render_to('m/event/list.html')
def mlist_test(request):
    if not request.is_mobile:
        return redirect('brispon.views.event.list_test')
    event_form = EventForm(request.GET or None)
    event_form.Meta.base_qs = Event.objects.filter(event_type='TEST')
    return paginate(request.GET, search_form=event_form, unit_page=12)
